package com.nxmu.hire.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nxmu.hire.common.exception.ServiceException;
import com.nxmu.hire.dao.PublicityMapper;
import com.nxmu.hire.model.Publicity;
import com.nxmu.hire.model.PublicityExample;
import com.nxmu.hire.service.PublicityService;
import com.nxmu.hire.vo.FindPublishByPageResVo;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PublicityServiceImpl implements PublicityService {

    private final static Logger LOGGER = LoggerFactory.getLogger(PublicityServiceImpl.class);

    @Autowired
    private PublicityMapper publicityMapper;

    /**
     * 添加发布信息
     * @param targetFile
     * @param inputStream
     */
    @Transactional
    @Override
    public void save(Publicity publicity, InputStream inputStream, File targetFile) throws ServiceException {

        try {
            // 1.上传文件
            FileUtils.copyInputStreamToFile(inputStream,targetFile);

            // 2.保存文件信息
            if (publicity.getPublishStatus() != null) publicity.setPublishDate(new Date());

            publicityMapper.insert(publicity);

        } catch (IOException e) {
            LOGGER.error("发布信息保存失败");
            throw new ServiceException("发布信息保存失败！");
        } catch (Exception e) {
            LOGGER.error("文件上传失败");
            throw new ServiceException("发布信息保存失败！");
        }
    }

    /**
     * 查询公示列表
     * @param headLine
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public  PageInfo<Publicity> findPublishByPageAndCondition(String headLine, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PublicityExample publicityExample = new PublicityExample();

        if (!StringUtils.isEmpty(headLine))  publicityExample.createCriteria().andHeadlineLike("%" + headLine + "%");
        List<Publicity> publicities = publicityMapper.selectByExampleWithBLOBs(publicityExample);
        return new PageInfo<>(publicities);
    }

    @Override
    public List<FindPublishByPageResVo> converterPublicityListToFindPublishByPageResVoList(List<Publicity> list) {
        List<FindPublishByPageResVo> resultList = new ArrayList<>();
        list.forEach(publicity -> {
            publicity.setAdditionName(null);
            publicity.setAdditionPath(null);
            FindPublishByPageResVo findPublishByPageResVo = new FindPublishByPageResVo();
            BeanUtils.copyProperties(publicity,findPublishByPageResVo);
            resultList.add(findPublishByPageResVo);
        });
        return resultList;
    }
}
